from PyQt6.QtWidgets import QGroupBox, QVBoxLayout, QLabel, QSizePolicy
from PyQt6.QtCore import Qt


class CaptureBox(QGroupBox):
    def __init__(self, title="截图"):
        super().__init__(title)

        # 设置布局
        layout = QVBoxLayout()
        self.setLayout(layout)

        # 添加一个标签用于显示截图
        self.captureLabel = QLabel("这是截图区域")
        self.captureLabel.setAlignment(Qt.AlignmentFlag.AlignCenter)
        # 设置尺寸策略，避免过度扩展
        self.captureLabel.setSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Preferred)
        layout.addWidget(self.captureLabel)

    def update_capture(self, pixmap):
        """更新截图显示"""
        if pixmap:
            # 缩放图片以适应标签大小，避免标签被撑大
            scaled_pixmap = pixmap.scaled(
                self.captureLabel.size(),
                Qt.AspectRatioMode.KeepAspectRatio,
                Qt.TransformationMode.SmoothTransformation
            )
            self.captureLabel.setPixmap(scaled_pixmap)
        else:
            self.captureLabel.setText("这是截图区域")
